/**
 * @param {number} x
 * @return {boolean}
 */
function anagrams(word, words) {
  word = word.split('')
  let sample = {}, res = [];
  word.forEach(item => { sample[item] == undefined ? sample[item] = 1 : sample[item]++ });
  words.forEach(element => {
    let flag = true
    let item = element.split('')
    let test = {}
    item.forEach(item => { test[item] == undefined ? test[item] = 1 : test[item]++ });
    if (Object.keys(test).length != Object.keys(sample).length) flag = false //console.log(Object.keys(test).length, element);
    for (key in sample) {
      if (sample[key] != test[key]) flag = false
    }
    if (flag) res.push(element)
  })
  return res
}

String.prototype.sort = function () {
  return this.split("").sort().join("");
};
function anagrams(word, words) {
  return words.filter(function (x) {
    return x.sort() === word.sort();
  });
}

function anagrams(word, words) {
  word = word.split('').sort().join('');
  return words.filter(function (v) { return word == v.split('').sort().join(''); });
}


let anagrams = (word, words) => words.filter(w => w.split('').sort().join('') === word.split('').sort().join(''));



function anagrams(word, words) {
  return words.filter(function (e) {
    return e.split('').sort().join('') === word.split('').sort().join('');
  })
}

console.log(anagrams('abba', ['aabb', 'abcd', 'bbaa', 'dada']))
console.log(anagrams('racer', ['crazer', 'carer', 'racar', 'caers', 'racer'])) //['carer', 'racer']
console.log(anagrams('laser', ['lazing', 'lazy', 'lacer']))